/**
 * Created by Administrator on 2016/2/23.
 * chrome扩展中的conntent javascript与普通页面的javascript只能共享dom，而不能相互调用函数
 */
(function(){

    /*
    * 当发送完毕，写自己的业务逻辑
    * */
    function on_send_complete(){

    }

    /*
    * 当发送完毕后，收到应答
    * */
    function  on_response(response){
        alert(JSON.stringify(response));
    }

    /*
    * 创建发送消息的client
    * */
//    var app_id="jfnpcfgkgbjpoeddbhcnenanfhpebghf";
//    var message_client=new MessageClient(app_id);
//    var message_client=new NativeMessageClient('qlrrprintservice')
//    //注册当发送完毕后，处理响应的回调函数
//    message_client.addResponseListener(function(response){
//        on_response(response);
//    });





    //测试打印图片的base64编码
    var img='Qk3mBwAAAAAAAD4AAAAoAAAAmQAAAGIAAAABAAEAAAAAAAAAAADEDgAAxA4AAAIAAAACAAAAAAAA////////////////////////////////gP////////////////////////+A/////////////////////////4D/////////////////////////gP////////////////////////+A/wAAAAAAAAAAAAAAAAAAAAAB/4D8AAAAAAAAAAAAAAAAAAAAAAB/gPj//////////////////////j+A8/+P/////////v//////////H4Dj/4/h/8f//4f8P//gD///x/+PgOffj+D/x///g/wH/8AH///H/8eAz8ePyH/j/48B/wD/gAf//8f/44DPg4/8f+P/j/H/gB+P4///x//zgM+Aj/x/8f+P8f+MB4/z///H//OAz4yAAH/x/4/x/44Aj/P//8f/84DPjoAAf/j/j/H/jiCP8f//x//zgM+PgAB/+P+P8f+OPI/z4AAAABOAz4+P/H/8/4/x/44/j//gAAAAE4DPj4/8f/z/j/H/jj+P/+AAAAATgM+PgAB//P+P8f+OP4/////H//OAz4+AAH/8f4/x/44/j////8f/84DPj4AAf/wAAAH/jj+P////x//zgM+Pj/x//AAAAf+OP4///wAAA/OAz4+AAH/8AAAB/44/j///AAAD84DPj4AAf/x/j/H/jj+H//8AAAPzgM+PgAB//H+P8f+OP4H//x/H4/OAz4/////8f4/x/44/gH//H8fj84DIDAAAD/x/j/H/jgCMP/8AAAPzgMgMAAAP/H+P8f+OAI4P/wAAA/OAyAwAAA/8AAAB/44Aj4f/AAAD84DP//8f//wAAAH/jj+Px/8fx+PzgM///x///AAAAf+OP4/v/x/H4/OAz/+AAH/8f4/x/44/j///AAAD84DP/4AAf/x/j/H//j+P//8AAAPzgM/PgAB//H+P8f/+P4///wAAA/OAz4//H//8f4/x//4/j////Pj/84DPDwAAP/x/j/H//j+P///4/H/zgM4fAAA//AAAAf/+P4////j+f/OAzn8AAD/8AAAB//4/j///8f4/84DO//8f//wAAAH//j+P///j/j/zgM///x///////////////+f/H/OAz///H///////////////7/9/84DP///////////////////////zgM////////////////////////OAz///////////////////////84DP///////////////////////zgM////////////////////////OAz///////////////////////84DP///////////////////////zgM////////////////////////OAz///////////////////////84DP///////////////////////zgM////////////////////////OAz///////////////////////84DP///////////////////////zgM////////////////////////OAz///////////////////////84DP///////////////////////zgM////////////////////////OAz////////x///3///////+f/E4DP////////H//+P//AH/j/4f4TgMgAAAAP//8f//8P/4AH+H/w/DOAyAAAAA///x///8f/AAf8P/x4c4DIAAAAD///H///4f8f4/4fnjDzgM//x/////8f///w/x/j/w8fAfOAz//H/////x////h/H/P/hh+D84DP/8f//4AAAAB//H8f8//EP4fzgM//x///gAAAAH/+Px/x/+B/x/OAz//H//+AAAAAf/8fH///4P+H84DP/8f/////H////58f///h/4PzgM//x/////8f////jx///8D/E/OAz//H/////x/////PH///iPcR84DP/8f///wAAA///8cf//8Y5znzgM//x////AAAD///xx///nziOfOAz//H///8AAAP///n///+/HI484DP/8f///x/H4///+P///f98HzzgM//x////H8fj/wAAAAB8//IfPOAz//AAH/8AAAP/AAAAAHx/5h884DP/8AAf/wAAA/8AAAAAfj/GHzzgM//wAB//AAAD///8////H88/HOAz//H///8fx+P///x///8fjx8c4DP/8f///x/H4////H///48fAATgM//x////AAAD///8f///v7+ABGAz//H///8AAAP///x///wAA4AEYDP/8f///wAAA////H///AADn/zgM//x/////Pj////8f//8AAOf/OAz//H////4/H////x////4/4/84DP/8f////j+f//AAAAH//j/j/zgM//x////8f4//8AAAAf/+P+P+eAz//H////j/j//wAAAB//4/4/x4DD//////+f/H/////////j/j+PgPH//////7/9///////////+/h+A+P/////////////////////8f4D8AAAAAAAAAAAAAAAAAAAAAAD/gP8AAAAAAAAAAAAAAAAAAAAAAf+A/////////////////////////4D/////////////////////////gP////////////////////////+A';

    //打印，自带PrintPaperTemplate，PrintPaperTemplateId可以为null
    var print_by_no_template=[
        {"Mm_to_dpi":8,//单位换算，200 DPI: 1 mm = 8 dots；300 DPI: 1 mm = 12 dots
            "PrintQRCodes":null,//二维码
            "Id":null,//打印ID，
            "PrintPaperTemplateId":null,//打印模板ID
            "PrintBarCodes":[{//打印条码，数组类型，可以添加多个条码
                "X":0,//x坐标，单位mm，int类型
                "Y":50,//y坐标，int类型
                "BarcodeHeight":20,//条码高度，int类型
                "IsPrintBarcodeFont":1,//是否打印二维码下的文字，int类型
                "Rotation":0,//旋转角度，int类型
                "Narrow":2,//条码窄bar比例因子,改值不设定也可以，默认为2，，int类型
                "Wide":2,//条码宽bar比例因子，int类型
                "Code":"1234567",//条码，string类型
                "BarcodeType":"128"//条码类型，string类型
            }],
            "PrintFonts"://打印文字，数组类型
                [{"X":0,
                    "Y":50,
                    "Fontheight":5,//文字高度,int类型
                    "Rotation":0,//旋转角度,int类型
                    "Fontstyle":2,//字体类型,int类型,0为标准,1为斜体，2为粗体，3为粗斜体
                    "Fontunderline":0,//底线，int类型，0为无底线，1为加底线
                    "SzFaceName":"黑体",//字体名称
                    "Content":"这是测试测试"//内容
                }],
            "PrintPaperTemplate":
            {"Id":null,
                "Size":{"M":95,"N":87},//打印纸的大小
                "Gap":{"M":3,"N":0},//两张打印纸间的间隙
                "Bline":{"M":0,"N":0},//設定黑標高度及使用者定義標籤印完後標籤額外送出的長度,m黑標高度,n額外送出紙張長度
                "Offset":{"M":0.00},//定義標籤於列印完後額外推出的長度，特別是在使用自動剝紙器或裁刀功能時，可用來調校標籤停止的位置，在列印下一張時印表機會將原先多推出或少推出的部份以回拉方式補償回來
                "Speed":null,//設定印表機的列印速度
                "Density":null,//設定印表機的列印濃度0~15
                "Direction":{"N":1,"M":0},//設定列印方向及鏡射功能，此設定將被記錄於 EEPROM
                "Reference":{"X":0,"Y":0},//定義標籤紙上的相對於原點的參考點座標
                "Feed":{"N":0},//將標籤紙向前推送指定的長度
                "BackFeed":{"N":0},//將標籤紙向後回拉指定的長
                "FormFeed":{},//將標籤向前推送至下一張標籤的起始位置
                "Home":{},//在使用含有間隙或黑標的標籤紙時，若不能確定第一張標籤紙是否在正確列印位置時，此指令可將標籤紙向前推送至下一張標籤紙的起點開始列印
                "PriltLines":[{"Width":90.00,"Height":2.00,"X":35,"Y":60}],//线，数组
                "PrintBitmaps":[{"Base64Code":img,"Mode":2,"X":60,"Y":60,"VHRotation":"V"}],//打印图片VHRotation，V，为垂直旋转，H水平旋转
                "PrintQRCodes":[{"QRCODE_ECC":"L",//錯誤糾正能力等級,L,M,Q,H
                    "CellWidth":4,//1~10
                    "Mode":"A",//自動生成編碼/手動生成編碼A:自动，M：手动
                    "Rotation":0,//旋转角度
                    "Model":1,//條碼生成樣式,一般不使用
                    "Mask":7,//範圍：0~8，預設7
                    "DataString":"334www",//条码内容
                    "X":80,"Y":60}]}}];

    var querystate={};

    var print_template={"Id":"test",
        "Size":{"M":96,"N":85},
        "Gap":{"M":3,"N":0},
        "Bline":{"M":0,"N":0},
        "Offset":{"M":0.0},
        "Speed":null,
        "Density":null,
        "Direction":{"M":0,"N":1},
        "Reference":{"X":0,"Y":0},
        "Feed":{"N":0},
        "BackFeed":{"N":0},
        "FormFeed":null,
        "Home":null,
        "PriltLines":[{"Width":100,"Height":0.50,"X":0,"Y":9},{"Width":100,"Height":0.50,"X":0,"Y":22},{"Width":0.50,"Height":13,"X":75,"Y":9}],
        "PrintBitmaps":null,
        "PrintQRCodes":null};

    var print_by_template=[{"Mm_to_dpi":8,
        "PrintQRCodes":null,
        "Id":null,
        "PrintPaperTemplateId":"test",
        "PrintBarCodes":[{"X":0,"Y":50,"BarcodeHeight":10,"IsPrintBarcodeFont":1,"Rotation":0,"Narrow":2,"Wide":2,"Code":"1234567","BarcodeType":"128"}],
        "PrintFonts":[{"X":0,"Y":70,"Fontheight":5,"Rotation":0,"Fontstyle":2,"Fontunderline":0,"SzFaceName":"黑体","Content":"这是测试测试"}],
        "PrintQRCodes":null
    }];

    function create_print_request_message(plugin_message_type,printer_type,message_body){
        return JSON.stringify({PluginMessageType:plugin_message_type,
            PrinterType : printer_type,
            MessageBody:JSON.stringify(message_body)
        });
    }

    //绑定add_print_template的click事件
    $("#add_print_template").click(function(){
        var message_client=new NativeMessageClient('qlrrprintservice')
        //注册当发送完毕后，处理响应的回调函数
        message_client.addResponseListener(function(response){
            on_response(response);
        });
        message_client.connect();
        var msg=create_print_request_message(4,1,print_template);
        message_client.sendMessage(msg);
    });


})(window);
